index.js ➔ realpath   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 3
dl 0
loc 17
rs 9.85
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ ... ➔ origRealpath 0 7 2
1
module.exports = realpath
2
realpath.realpath = realpath
3
realpath.sync = realpathSync
4
realpath.realpathSync = realpathSync
5
realpath.monkeypatch = monkeypatch
6
realpath.unmonkeypatch = unmonkeypatch
7
8
var fs = require('fs')
9
var origRealpath = fs.realpath
10
var origRealpathSync = fs.realpathSync
11
12
var version = process.version
13
var ok = /^v[0-5]\./.test(version)
14
var old = require('./old.js')
15
16
function newError (er) {
17
  return er && er.syscall === 'realpath' && (
18
    er.code === 'ELOOP' ||
19
    er.code === 'ENOMEM' ||
20
    er.code === 'ENAMETOOLONG'
21
  )
22
}
23
24
function realpath (p, cache, cb) {
25
  if (ok) {
26
    return origRealpath(p, cache, cb)
27
  }
28
29
  if (typeof cache === 'function') {
30
    cb = cache
31
    cache = null
32
  }
33
  origRealpath(p, cache, function (er, result) {
34
    if (newError(er)) {
35
      old.realpath(p, cache, cb)
36
    } else {
37
      cb(er, result)
38
    }
39
  })
40
}
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
41
42
function realpathSync (p, cache) {
43
  if (ok) {
44
    return origRealpathSync(p, cache)
45
  }
46
47
  try {
48
    return origRealpathSync(p, cache)
49
  } catch (er) {
50
    if (newError(er)) {
51
      return old.realpathSync(p, cache)
52
    } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
53
      throw er
54
    }
55
  }
56
}
57
58
function monkeypatch () {
59
  fs.realpath = realpath
60
  fs.realpathSync = realpathSync
61
}
62
63
function unmonkeypatch () {
64
  fs.realpath = origRealpath
65
  fs.realpathSync = origRealpathSync
66
}
67